<?php
if(!defined('BLOG'))exit('不作死就不会死');
/**
 *  mysql.class.php 数据库类
 *
 * @copyright
 * @license
 * @lastmodify
 */
final class mysql{
    private $config = null;
    public $link = null;

    /**
     * @param $config
     */
    public function __construct($config){
        $this->config = $config;
        return $this->connect();
    }

    /**
     * @return mysqli|null
     */
    public function connect(){
        if(!$this->link){
            $this->link = mysqli_connect($this->config['db_host'],$this->config['db_user'],$this->config['db_passwd'],$this->config['db_name'],$this->config['db_port']) or die(L('db_connect_fail'));
            mysqli_set_charset($this->link,$this->config['charset']);
            return $this->link;
        }
    }

    /**
     * @param $data
     * @param $table
     * @param string $where
     * @param string $order
     * @param string $limit
     * @param string $group
     * @return array|bool|mysqli_result
     */
    public function select($data,$table,$where='',$order='',$limit='',$group=''){
        $data = is_array($data)?implode(',',$data):$data;
        $sql = 'SELECT '.$data.' FROM '.$table.' '.$where.' '.$order.' '.$limit.$group;
        $result =  $this->query($sql);
        $array = array();
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            $array[] = $row;
        }
        mysqli_free_result($result);
        return $array;
    }

    /**
     * @param $data
     * @param $table
     * @param string $where
     * @return array|bool|mysqli_result
     */
    public function get_one($data,$table,$where=''){
        $data = is_array($data)?implode(',',$data):$data;
        $sql = 'SELECT '.$data.' FROM '.$table.' '.$where.' limit 0,1';
        $result =  $this->query($sql);
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        mysqli_free_result($result);
        return $row;


    }
    /**
     * @param $where
     * @param $data
     * @param $table
     * @return array|bool|mysqli_result
     */
    public function update($where,$data,$table){
        if(is_array($data)){
            $paresql = '';
            foreach($data as $key => $val){
                $paresql.=$key.' = '.'\''.$val.'\'';
            }
            $data = $paresql;
        }
        $sql = 'UPDATE '.$table.' SET '.$data.' where '.$where;
        return $this->query($sql);
    }

    /**
     * @param $data
     * @param $table
     * @return array|bool|mysqli_result
     */
    public function insert($data,$table){
        if(is_array($data)){
            $keys = array_keys($data);
            $values=array();$columns=array(); $i = 0;
            foreach($data as $key => $val){
                $values[]='\''.$val.'\'';
                $columns[]='`'.$keys[$i].'`';
                $i++;
            }
            $column = implode(',',$columns);
            $value = implode(',',$values);
            $data =  '('. $column .') VALUES '.'('.$value.')';
        }
        $sql = "INSERT INTO ".$table.' '.$data;
       return $this->query($sql);
    }

    /**
     * @param $where
     * @param $table
     * @return array|bool|mysqli_result
     */
    public function delete($where,$table){
        $sql = "DELETE FROM ".$table.' '.$where;
        return $this->query($sql);
    }

    /**
     * @param $sql
     * @return bool|mysqli_result
     */
    public function query($sql){
        $rs =  mysqli_query($this->link,$sql);
        if(!$rs){
            $this->debug($sql);
        }
        return $rs;
    }

    /**
     * @return int|string
     */
    public function insertid(){
        return mysqli_insert_id ($this->link);
    }
    /**
     * @param $result
     * @return array
     */
    public function resultToarray($result){
        $array = array();
            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                $array[] = $row;
            }
        mysqli_free_result($result);
        return $array;
    }

    /**
     * @param $sql
     */
    public function debug($sql){
        if($this->config['debug'])echo $sql.'<br />'.str_replace(ROOTPATH,'',mysqli_error($this->link));exit;
    }

}